﻿using Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using ServiceCore.Tool.Post;
using ServiceCore.Tool.Wx.wx_media;
using System;
using System.Collections.Generic;
using System.Text;

namespace ServiceCore.Tool.Wx.media
{

    public interface Iwx_media_service
    {
        /// <summary>
        /// 新增永久图文素材
        /// </summary>
        /// <param name="add_list"></param>
        /// <returns></returns>
        add_news_result add_news(List<add_news_articles_item> add_list);
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="media"></param>
        /// <returns></returns>
        uploadimg_result uploadimg(IFormFile media);
        /// <summary>
        ///  新增其他类型永久素材
        /// </summary>
        /// <param name="media"></param>
        /// <param name="type"></param>
        /// <param name="title"></param>
        /// <param name="introduction"></param>
        /// <returns></returns>
        add_material_result add_material(IFormFile media, string type, string title, string introduction);
        /// <summary>
        /// 获取永久素材
        /// </summary>
        /// <param name="media_id"></param>
        /// <returns></returns>
        get_material_result get_material(string media_id);
        /// <summary>
        /// 获取素材列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        batchget_material_result batchget_material(string type, int count, int offset);
        /// <summary>
        /// 删除素材列表
        /// </summary>
        del_material_result del_material(string media_id);

        /// <summary>
        /// 获取永久素材列表数量
        /// </summary>
        get_materialcount_result get_materialcount();

        /// <summary>
        /// 修改图文消息
        /// </summary>
        update_news_result update_news(update_news_params news_model);
    }
    public class wx_media_service : Iwx_media_service
    {
        private readonly IConfiguration AppSettingConfig;
        public wx_media_service(IConfiguration configuration)
        {
            AppSettingConfig = configuration;
        }
        public string get_access_token_api_url { get { return AppSettingConfig["get_access_token_api"]; } }
        /// <summary>
        /// 获取accesstoken
        /// </summary>
        private string get_accesstoken
        {
            get
            {
                string access_token = Http_Helper.HttpGet(get_access_token_api_url + $"?who=rk_{DateTime.Now.ToString("yyyy-MM:dd")}");
                if (access_token == "小朋友，你是不是有许多问号")
                {
                    access_token = "";
                }
                return access_token;
            }
        }
        #region  请求方法
        /// <summary>
        /// 新增永久图文素材
        /// </summary>
        public add_news_result add_news(List<add_news_articles_item> add_list)
        {
            add_news_result wx_result = new api_base().create_option(media_config.Api_Url.add_news, get_accesstoken).http_client<add_news_params, add_news_result>(new add_news_params()
            {
                articles = add_list
            }, false);
            return wx_result;
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        public uploadimg_result uploadimg(IFormFile media)
        {
            uploadimg_result wx_result = new api_base().create_option(media_config.Api_Url.uploadimg, get_accesstoken).post_upload<uploadimg_params, uploadimg_result>(media);
            return wx_result;
        }
        /// <summary>
        /// 新增其他类型永久素材
        /// </summary>
        public add_material_result add_material(IFormFile media, string type, string title, string introduction)
        {
            Dictionary<string, string> get_ext_params = new Dictionary<string, string>();
            get_ext_params.Add("type", type);
            Dictionary<string, string> post_ext_params = new Dictionary<string, string>();
            get_ext_params.Add("description", Newtonsoft.Json.JsonConvert.SerializeObject(new { title, introduction })); 
            add_material_result wx_result = new api_base().create_option(media_config.Api_Url.add_material, get_accesstoken).post_upload<uploadimg_params, add_material_result>(media, get_ext_params, post_ext_params);
            return wx_result;
        }


        /// <summary>
        /// 获取永久素材 只对接了多图文以及视频
        /// </summary>
        public get_material_result get_material(string media_id)
        { 
            get_material_result wx_result = new api_base().create_option(media_config.Api_Url.get_material, get_accesstoken).http_client<get_material_params, get_material_result>(new get_material_params()
            {
                media_id = media_id
            }, false);
            return wx_result;
        }
        /// <summary>
        /// 获取素材列表
        /// </summary>
        public batchget_material_result batchget_material(string type, int count, int offset)
        {
            batchget_material_result wx_result = new api_base().create_option(media_config.Api_Url.batchget_material, get_accesstoken).http_client<batchget_material_params, batchget_material_result>(new batchget_material_params()
            {
                type = type,
                count = count,
                offset = offset
            }, false);
            return wx_result;
        }

        /// <summary>
        /// 获取永久素材列表数量
        /// </summary>
        public get_materialcount_result get_materialcount()
        {
            get_materialcount_result wx_result = new api_base().create_option(media_config.Api_Url.get_materialcount, get_accesstoken).http_client<DBNull, get_materialcount_result>(null);
            return wx_result;
        }
        /// <summary>
        /// 删除素材列表
        /// </summary>
        public del_material_result del_material(string media_id)
        {
            del_material_result wx_result = new api_base().create_option(media_config.Api_Url.del_material, get_accesstoken).http_client<del_material_params, del_material_result>(new del_material_params()
            {
                media_id = media_id
            }, false);
            return wx_result;
        }

        /// <summary>
        /// 修改图文消息
        /// </summary>
        public update_news_result update_news(update_news_params news_model)
        {
            update_news_result wx_result = new api_base().create_option(media_config.Api_Url.update_news, get_accesstoken).http_client<update_news_params, update_news_result>(news_model, false);
            return wx_result;
        }
        #endregion
    }
}
